// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

using Elastic.Clients.Elasticsearch.Serverless.Fluent;
using Elastic.Clients.Elasticsearch.Serverless.Serialization;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Serverless.MachineLearning;

public sealed partial class DetectionRule
{
	/// <summary>
	/// <para>The set of actions to be triggered when the rule applies. If more than one action is specified the effects of all actions are combined.</para>
	/// </summary>
	[JsonInclude, JsonPropertyName("actions")]
	public ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleAction>? Actions { get; set; }

	/// <summary>
	/// <para>An array of numeric conditions when the rule applies. A rule must either have a non-empty scope or at least one condition. Multiple conditions are combined together with a logical AND.</para>
	/// </summary>
	[JsonInclude, JsonPropertyName("conditions")]
	public ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleCondition>? Conditions { get; set; }

	/// <summary>
	/// <para>A scope of series where the rule applies. A rule must either have a non-empty scope or at least one condition. By default, the scope includes all series. Scoping is allowed for any of the fields that are also specified in `by_field_name`, `over_field_name`, or `partition_field_name`.</para>
	/// </summary>
	[JsonInclude, JsonPropertyName("scope")]
	public IDictionary<Elastic.Clients.Elasticsearch.Serverless.Field, Elastic.Clients.Elasticsearch.Serverless.MachineLearning.FilterRef>? Scope { get; set; }
}

public sealed partial class DetectionRuleDescriptor<TDocument> : SerializableDescriptor<DetectionRuleDescriptor<TDocument>>
{
	internal DetectionRuleDescriptor(Action<DetectionRuleDescriptor<TDocument>> configure) => configure.Invoke(this);

	public DetectionRuleDescriptor() : base()
	{
	}

	private ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleAction>? ActionsValue { get; set; }
	private ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleCondition>? ConditionsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor ConditionsDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor> ConditionsDescriptorAction { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor>[] ConditionsDescriptorActions { get; set; }
	private IDictionary<Elastic.Clients.Elasticsearch.Serverless.Field, Elastic.Clients.Elasticsearch.Serverless.MachineLearning.FilterRefDescriptor> ScopeValue { get; set; }

	/// <summary>
	/// <para>The set of actions to be triggered when the rule applies. If more than one action is specified the effects of all actions are combined.</para>
	/// </summary>
	public DetectionRuleDescriptor<TDocument> Actions(ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleAction>? actions)
	{
		ActionsValue = actions;
		return Self;
	}

	/// <summary>
	/// <para>An array of numeric conditions when the rule applies. A rule must either have a non-empty scope or at least one condition. Multiple conditions are combined together with a logical AND.</para>
	/// </summary>
	public DetectionRuleDescriptor<TDocument> Conditions(ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleCondition>? conditions)
	{
		ConditionsDescriptor = null;
		ConditionsDescriptorAction = null;
		ConditionsDescriptorActions = null;
		ConditionsValue = conditions;
		return Self;
	}

	public DetectionRuleDescriptor<TDocument> Conditions(Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor descriptor)
	{
		ConditionsValue = null;
		ConditionsDescriptorAction = null;
		ConditionsDescriptorActions = null;
		ConditionsDescriptor = descriptor;
		return Self;
	}

	public DetectionRuleDescriptor<TDocument> Conditions(Action<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor> configure)
	{
		ConditionsValue = null;
		ConditionsDescriptor = null;
		ConditionsDescriptorActions = null;
		ConditionsDescriptorAction = configure;
		return Self;
	}

	public DetectionRuleDescriptor<TDocument> Conditions(params Action<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor>[] configure)
	{
		ConditionsValue = null;
		ConditionsDescriptor = null;
		ConditionsDescriptorAction = null;
		ConditionsDescriptorActions = configure;
		return Self;
	}

	/// <summary>
	/// <para>A scope of series where the rule applies. A rule must either have a non-empty scope or at least one condition. By default, the scope includes all series. Scoping is allowed for any of the fields that are also specified in `by_field_name`, `over_field_name`, or `partition_field_name`.</para>
	/// </summary>
	public DetectionRuleDescriptor<TDocument> Scope(Func<FluentDescriptorDictionary<Elastic.Clients.Elasticsearch.Serverless.Field, Elastic.Clients.Elasticsearch.Serverless.MachineLearning.FilterRefDescriptor>, FluentDescriptorDictionary<Elastic.Clients.Elasticsearch.Serverless.Field, Elastic.Clients.Elasticsearch.Serverless.MachineLearning.FilterRefDescriptor>> selector)
	{
		ScopeValue = selector?.Invoke(new FluentDescriptorDictionary<Elastic.Clients.Elasticsearch.Serverless.Field, Elastic.Clients.Elasticsearch.Serverless.MachineLearning.FilterRefDescriptor>());
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (ActionsValue is not null)
		{
			writer.WritePropertyName("actions");
			JsonSerializer.Serialize(writer, ActionsValue, options);
		}

		if (ConditionsDescriptor is not null)
		{
			writer.WritePropertyName("conditions");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, ConditionsDescriptor, options);
			writer.WriteEndArray();
		}
		else if (ConditionsDescriptorAction is not null)
		{
			writer.WritePropertyName("conditions");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor(ConditionsDescriptorAction), options);
			writer.WriteEndArray();
		}
		else if (ConditionsDescriptorActions is not null)
		{
			writer.WritePropertyName("conditions");
			writer.WriteStartArray();
			foreach (var action in ConditionsDescriptorActions)
			{
				JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor(action), options);
			}

			writer.WriteEndArray();
		}
		else if (ConditionsValue is not null)
		{
			writer.WritePropertyName("conditions");
			JsonSerializer.Serialize(writer, ConditionsValue, options);
		}

		if (ScopeValue is not null)
		{
			writer.WritePropertyName("scope");
			JsonSerializer.Serialize(writer, ScopeValue, options);
		}

		writer.WriteEndObject();
	}
}

public sealed partial class DetectionRuleDescriptor : SerializableDescriptor<DetectionRuleDescriptor>
{
	internal DetectionRuleDescriptor(Action<DetectionRuleDescriptor> configure) => configure.Invoke(this);

	public DetectionRuleDescriptor() : base()
	{
	}

	private ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleAction>? ActionsValue { get; set; }
	private ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleCondition>? ConditionsValue { get; set; }
	private Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor ConditionsDescriptor { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor> ConditionsDescriptorAction { get; set; }
	private Action<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor>[] ConditionsDescriptorActions { get; set; }
	private IDictionary<Elastic.Clients.Elasticsearch.Serverless.Field, Elastic.Clients.Elasticsearch.Serverless.MachineLearning.FilterRefDescriptor> ScopeValue { get; set; }

	/// <summary>
	/// <para>The set of actions to be triggered when the rule applies. If more than one action is specified the effects of all actions are combined.</para>
	/// </summary>
	public DetectionRuleDescriptor Actions(ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleAction>? actions)
	{
		ActionsValue = actions;
		return Self;
	}

	/// <summary>
	/// <para>An array of numeric conditions when the rule applies. A rule must either have a non-empty scope or at least one condition. Multiple conditions are combined together with a logical AND.</para>
	/// </summary>
	public DetectionRuleDescriptor Conditions(ICollection<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleCondition>? conditions)
	{
		ConditionsDescriptor = null;
		ConditionsDescriptorAction = null;
		ConditionsDescriptorActions = null;
		ConditionsValue = conditions;
		return Self;
	}

	public DetectionRuleDescriptor Conditions(Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor descriptor)
	{
		ConditionsValue = null;
		ConditionsDescriptorAction = null;
		ConditionsDescriptorActions = null;
		ConditionsDescriptor = descriptor;
		return Self;
	}

	public DetectionRuleDescriptor Conditions(Action<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor> configure)
	{
		ConditionsValue = null;
		ConditionsDescriptor = null;
		ConditionsDescriptorActions = null;
		ConditionsDescriptorAction = configure;
		return Self;
	}

	public DetectionRuleDescriptor Conditions(params Action<Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor>[] configure)
	{
		ConditionsValue = null;
		ConditionsDescriptor = null;
		ConditionsDescriptorAction = null;
		ConditionsDescriptorActions = configure;
		return Self;
	}

	/// <summary>
	/// <para>A scope of series where the rule applies. A rule must either have a non-empty scope or at least one condition. By default, the scope includes all series. Scoping is allowed for any of the fields that are also specified in `by_field_name`, `over_field_name`, or `partition_field_name`.</para>
	/// </summary>
	public DetectionRuleDescriptor Scope(Func<FluentDescriptorDictionary<Elastic.Clients.Elasticsearch.Serverless.Field, Elastic.Clients.Elasticsearch.Serverless.MachineLearning.FilterRefDescriptor>, FluentDescriptorDictionary<Elastic.Clients.Elasticsearch.Serverless.Field, Elastic.Clients.Elasticsearch.Serverless.MachineLearning.FilterRefDescriptor>> selector)
	{
		ScopeValue = selector?.Invoke(new FluentDescriptorDictionary<Elastic.Clients.Elasticsearch.Serverless.Field, Elastic.Clients.Elasticsearch.Serverless.MachineLearning.FilterRefDescriptor>());
		return Self;
	}

	protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
	{
		writer.WriteStartObject();
		if (ActionsValue is not null)
		{
			writer.WritePropertyName("actions");
			JsonSerializer.Serialize(writer, ActionsValue, options);
		}

		if (ConditionsDescriptor is not null)
		{
			writer.WritePropertyName("conditions");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, ConditionsDescriptor, options);
			writer.WriteEndArray();
		}
		else if (ConditionsDescriptorAction is not null)
		{
			writer.WritePropertyName("conditions");
			writer.WriteStartArray();
			JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor(ConditionsDescriptorAction), options);
			writer.WriteEndArray();
		}
		else if (ConditionsDescriptorActions is not null)
		{
			writer.WritePropertyName("conditions");
			writer.WriteStartArray();
			foreach (var action in ConditionsDescriptorActions)
			{
				JsonSerializer.Serialize(writer, new Elastic.Clients.Elasticsearch.Serverless.MachineLearning.RuleConditionDescriptor(action), options);
			}

			writer.WriteEndArray();
		}
		else if (ConditionsValue is not null)
		{
			writer.WritePropertyName("conditions");
			JsonSerializer.Serialize(writer, ConditionsValue, options);
		}

		if (ScopeValue is not null)
		{
			writer.WritePropertyName("scope");
			JsonSerializer.Serialize(writer, ScopeValue, options);
		}

		writer.WriteEndObject();
	}
}